package com.lemon.exam.entity.po;

import com.lemon.exam.common.annotation.LogicDelete;
import com.lemon.exam.entity.vo.PaperVO;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.validator.constraints.Range;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 试卷表 持久层对象
 *
 * @author Lemon
 * @since 2025/05/11 13:47:13
 */
@Getter
@Setter
@ToString
@Table(value = "biz_paper")
@Schema(name = "试卷表")
public class PaperPO implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    @Id
    @Schema(description = "试卷主键ID")
    @Column(value = "id")
    private Long id;

    @Schema(description = "年级ID")
    @Column(value = "grade_id")
    private Long gradeId;

    @Schema(description = "班级ID")
    @Column(value = "class_id")
    @NotNull(message = "请选择班级")
    private Long classId;

    @Schema(description = "名称")
    @Column(value = "name")
    @NotBlank(message = "试卷名称不能为空")
    private String name;

    @Schema(description = "考试说明")
    @Column(value = "remark")
    private String remark;

    @Schema(description = "创建人ID")
    @Column(value = "create_id")
    private Long createId;

    @Schema(description = "创建人")
    @Column(value = "create_by")
    private String createBy;

    @Schema(description = "创建时间")
    @Column(value = "create_time")
    private LocalDateTime createTime;

    @Schema(description = "开始考试时间")
    @Column(value = "start_time")
    @NotNull(message = "请选择考试开始时间")
    private LocalDateTime startTime;

    @Schema(description = "结束考试时间")
    @Column(value = "end_time")
    @NotNull(message = "请选择考试结束时间")
    private LocalDateTime endTime;

    @Schema(description = "时长（分钟）")
    @Column(value = "duration")
    @NotNull(message = "请输入考试时长")
    //@Min(value = 1, message = "考试时长必须大于0")
    @Range(min = 1, max = 240, message = "考试时长必须在1-240分钟之间")
    private Integer duration;

    @Schema(description = "是否允许查看答案 1是 0否")
    @Column(value = "look_answer")
    @NotNull(message = "请选择是否允许查看答案")
    private Integer lookAnswer;

    @Schema(description = "是否打乱试题 1是 0否")
    @Column(value = "upset")
    @NotNull(message = "请选择是否打乱试题")
    private Integer upset;

    @Schema(description = "及格分数")
    @Column(value = "pass_score")
    @NotNull(message = "请输入及格分数")
    private Double passScore;

    @Schema(description = "试卷总分")
    @Column(value = "total_score")
    @NotNull(message = "请输入试卷总分")
    private Double totalScore;

    @Schema(description = "原始文件名")
    @Column(value = "origin_file_name")
    @NotBlank(message = "请上传试卷")
    private String originFileName;

    @Schema(description = "新文件名")
    @Column(value = "new_file_name")
    @NotBlank(message = "请上传试卷")
    private String newFileName;

    @Schema(description = "文件路径")
    @Column(value = "file_path")
    @NotBlank(message = "请上传试卷")
    private String filePath;

    @Schema(description = "删除：0正常 1删除")
    @Column(value = "deleted")
    @LogicDelete
    private Integer deleted;

    @Transient
    @Schema(description = "班级名称")
    private String className;

    /**
     * 转换为VO
     *
     * @return
     */
    public PaperVO toPaperVO() {
        PaperVO vo = new PaperVO();
        vo.setId(this.getId());
        vo.setName(this.getName());
        vo.setRemark(this.getRemark());
        vo.setStartTime(this.getStartTime());
        vo.setEndTime(this.getEndTime());
        vo.setDuration(this.getDuration());
        vo.setPassScore(this.getPassScore());
        vo.setTotalScore(this.getTotalScore());
        vo.setLookAnswer(this.getLookAnswer());
        vo.setUpset(this.getUpset());
        return vo;
    }
}
